package itheima.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import itheima.common.R;
import itheima.dto.DishDto;
import itheima.dto.SetmealDto;
import itheima.entity.Category;
import itheima.entity.Setmeal;
import itheima.service.CategorySerevice;
import itheima.service.SetmealDishService;
import itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 * @author shkstart
 * @create 2022-09-19-16:07
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategorySerevice categorySerevice;

    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("套餐信息：{}",setmealDto);

        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功！");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page (int page,int pageSize,String name){
        //分页构造器对象
        Page<Setmeal> st = new Page<>(page,pageSize);

        Page<SetmealDto> dtoPage = new Page<>();


        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        //添加查询条件，根据name进行like模糊查询
        lambdaQueryWrapper.like(name != null,Setmeal::getName,name);
        //添加排序条件，根据更新时间降序排序
        lambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);


        setmealService.page(st,lambdaQueryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(st,dtoPage,"records");
        List<Setmeal> records = st.getRecords();
       List<SetmealDto> list =  records.stream().map((item)-> {

            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            //分类id
            Long categoryId = item.getCategoryId();
            //根据分类id查询分类对象
            Category category = categorySerevice.getById(categoryId);

            if (category != null) {

                //分类对象
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());

       dtoPage.setRecords(list);
        return R.success(dtoPage);
    }

    /**
     * 根据id删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){

        setmealService.removeSetme(ids);
        return R.success("删除成功！");
    }

    @PostMapping("/status/{status}")
    public R<String> Status(@PathVariable Integer status,Long[] ids){
        //Arrays.asList:数组转换成集合
        List<Long> longs = Arrays.asList(ids);

        LambdaUpdateWrapper<Setmeal> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(Setmeal::getStatus,status).in(Setmeal::getId,longs);
        setmealService.update(lambdaUpdateWrapper);

        return R.success("修改成功过");
    }


   @GetMapping("/{id}")
    public R<SetmealDto> get(@PathVariable Long id){

       SetmealDto wIthDish = setmealService.getWIthDish(id);


       return R.success(wIthDish);

   }

    /**
     * 修改套餐
     * @param setmealDto
     * @return
     */
   @PutMapping
   public R<String> update(@RequestBody SetmealDto setmealDto){
       log.info("返回的是{}",setmealDto);
       setmealService.updateWith(setmealDto);
        return R.success("修改成功");

   }

    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
   @GetMapping("/list")
   public R<List<Setmeal>> list(Setmeal setmeal){
       //setmeal/list?categoryId=1413342269393674242&status=1：是这种拼接的话就不用注解
       //因为查询的数据Setmeal表中都有所以使用Setmeal
       LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
       lambdaQueryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
       lambdaQueryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
       lambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);

       List<Setmeal> setmeals = setmealService.list(lambdaQueryWrapper);
       return R.success(setmeals);
   }
}
